﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace TccServicos.Util
{
    public static class Log
    {
        private static Object thisLock = new Object();

        public static string GravarLog(string mensagem)
        {
            if (mensagem.Contains("Erro"))
            {
                //string pasta = Environment.CurrentDirectory;
                string pasta = System.Web.HttpContext.Current.Server.MapPath("~/");
                StringBuilder sb = new StringBuilder();

                sb.Append(DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00") +
                              DateTime.Now.Day.ToString("00") + " " + DateTime.Now.Hour.ToString("00") + ":" +
                              DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00") + " - ");
                sb.AppendLine(mensagem);
                //sb.AppendLine("------------------------------------------------------------------");
                //sb.AppendLine();

                string data = DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00") +
                              DateTime.Now.Day.ToString("00");
                string arquivo = pasta + @"\Log_" + data + ".log";

                try
                {
                    lock (thisLock)
                    {
                        using (StreamWriter outfile = new StreamWriter(arquivo, true))
                        {
                            outfile.Write(sb.ToString());
                        }
                    }
                }
                catch
                { }

                return arquivo;
            }
            return "";
        }
    }
}